// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 



using System.Collections.Generic;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.UiCore.KineticControls;

namespace svs.Mobile.BeyondMedia.Forms.Settings
{
	/// <summary>
	/// Provide a 2 line listview
	/// </summary>
	internal class KineticSettingsListView : KineticListView
	{
		#region Delegates

		public delegate void PageSelectedEvent(Settings.SettingPage page);

		#endregion

		private readonly List<Settings.SettingPage> m_Items = new List<Settings.SettingPage>();

		protected override void OnCustomizeControlSettings(svs.Mobile.BeyondMedia.UiCore.KineticControls.ListViewSettings settings)
		{
			settings.PrimaryIconWidth = 0;
		}

		public Settings.SettingPage SelectedPage
		{
			get
			{
				if (SelectedIndex != -1 && m_Items.Count > 0)
					return m_Items[SelectedIndex];

				return new Settings.SettingPage(null, null, SettingsPageHolderForm.SettingsPages.General);
			}
			set
			{
				int i = m_Items.IndexOf(value);

				if (SelectedIndex != i)
				{
					SelectedIndex = i;
				}

				ScrollItemIntoView(SelectedIndex);
			}
		}

		public event PageSelectedEvent PageSelected;

		public void SetPages(List<Settings.SettingPage> value)
		{
			Clear();
			m_Items.AddRange(value);
			SetItems(BuildNativeControlItems(), 0, false);
		}

		protected override List<ListViewItemData> BuildNativeControlItems()
		{
			var retVal = new List<ListViewItemData>(m_Items.Count);

			foreach (var settingsPage in m_Items)
			{
				retVal.Add(BuiltNativeControlItem(settingsPage));
			}

			return retVal;
		}

		private ListViewItemData BuiltNativeControlItem(Settings.SettingPage page)
		{
			var i = new ListViewItemData
								{
									Group = "BeyondPod Settings",
									PrimaryText = page.PageName,
									SecondaryText = page.PageDescription,
									SecondaryImage = FeedImageList.MoreArrowImage,
								};

			return i;
		}

		public void Reload()
		{
			SetItems(BuildNativeControlItems(), SelectedIndex, false);
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Return:
					e.Handled = true;
					OnPageSelected();
					break;

				case Keys.Left:
					e.Handled = true;
					break;

				case Keys.Right:
					e.Handled = true;
					OnPageSelected();
					break;
			}

			if (!e.Handled)
			{
				base.OnKeyDown(e);
			}
		}

		protected override void OnKeyUp(KeyEventArgs e)
		{
			// Ignore the Up as the selection is done on Down
		}

		protected override void OnBackKeyClicked()
		{
			SelectedIndex = -1;
			OnPageSelected();
		}

		protected override void OnItemBodyClicked(int index)
		{
			OnPageSelected();
		}

		private void OnPageSelected()
		{
			if (PageSelected != null)
			{
				PageSelected(SelectedPage);
			}
		}

		/// <summary>
		/// Clears this instance.
		/// </summary>
		public void Clear()
		{
			SelectedIndex = -1;
			m_Items.Clear();
		}
	}
}